package com.robam.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.robam.common.constant.ProductConstant;
import com.robam.gulimall.product.vo.AttrRespVo;
import com.robam.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.robam.gulimall.product.entity.AttrEntity;
import com.robam.gulimall.product.service.AttrService;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.R;



/**
 * 商品属性
 *
 * @author bobo
 * @email 412467850@qq.com
 * @date 2020-10-15 08:42:51
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    /**
     * 根据三级分类id获取基本属性(规格参数)
     * @param params
     * @param catId
     * @return
     */
    @GetMapping("/base/getByCatId/{catId}")
    public R getBaseAttrByCatId(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
        int attrType = ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode();
        PageUtils page = attrService.getAttrByCatId(params, catId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 根据三级分类id获取销售属性
     * @param params
     * @param catId
     * @return
     */
    @GetMapping("/sale/getByCatId/{catId}")
    public R getSaleAttrByCatId(@RequestParam Map<String, Object> params, @PathVariable("catId") Long catId) {
        int attrType = ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode();
        PageUtils page = attrService.getAttrByCatId(params, catId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
